package com.example.aicode.tool;

import dev.langchain4j.memory.ChatMemory;
import dev.langchain4j.memory.chat.MessageWindowChatMemory;
import dev.langchain4j.model.chat.ChatModel;
import dev.langchain4j.model.chat.StreamingChatModel;
import dev.langchain4j.rag.content.retriever.ContentRetriever;
import dev.langchain4j.service.AiServices;
import jakarta.annotation.Resource;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class AiCodeServiceFactory {

    @Resource(name = "myQwenChatModel")
    private ChatModel myqwenChatModel;

    @Resource
    private ContentRetriever contentRetriever;

    @Resource
    private StreamingChatModel qwenStreamingChatModel;

    @Bean
    public AiCodeService aiCodeService(){

//        会话记忆(最多保存n条记录)-(默认是存储在内存里的-项目重启会消失)
        ChatMemory chatMemory = MessageWindowChatMemory.withMaxMessages(10);

        AiCodeService aiCodeService = AiServices.builder(AiCodeService.class)
                .chatModel(myqwenChatModel)
                .streamingChatModel(qwenStreamingChatModel) //流式聊天模型
                .chatMemoryProvider(memoryId -> MessageWindowChatMemory.withMaxMessages(10))  //每个会话独立存储
                .chatMemory(chatMemory)
                .contentRetriever(contentRetriever) //RAG检索增强生成

                .chatMemoryProvider(memoryId -> MessageWindowChatMemory.withMaxMessages(10))    // 实现会话记忆隔离的方式
                .build();
        return aiCodeService;
//        return  AiServices.create(AiCodeService.class,qwenChatModel);
    }

}
